package fonkin.com.wifisignapp.adapter.news;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Collection;
import java.util.List;

import fonkin.com.wifisignapp.R;
import fonkin.com.wifisignapp.bean.tissue.Department;

/**
 * @Description:部门列表Adapter
 * @Author: huanghongjing
 * @Date: 2018/4/7 23:43
 * No such property: code for class: Script1
 *
 */

public class WifiAdapter extends BaseAdapter {
    private Context context;
    private List<ScanResult> wifiList;

    public WifiAdapter(Context context, List<ScanResult> wifiList) {
        this.context = context;
        this.wifiList = wifiList;
    }

    @Override
    public int getCount() {
        return wifiList.size();
    }

    @Override
    public Object getItem(int position) {
        return wifiList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.wifi_listitem,parent,false);
            convertView.setTag(new Viewholder(convertView));
        }
        Viewholder tag = (Viewholder) convertView.getTag();

        ScanResult scanResult = wifiList.get(position);
        tag.ssid.setText(scanResult.SSID);
        int levleLenght = Math.abs(scanResult.level);
        if (levleLenght<60){
            tag.wifiLevel.setImageResource(R.drawable.wifi_level4);
        }else if(60<=levleLenght&&levleLenght<=75){
            tag.wifiLevel.setImageResource(R.drawable.wifi_level3);
        }else if(75<=levleLenght&&levleLenght<=85){
            tag.wifiLevel.setImageResource(R.drawable.wifi_level2);
        }else {
            tag.wifiLevel.setImageResource(R.drawable.wifi_level1);
        }
        /*int level= WifiManager.calculateSignalLevel(scanResult.level,5);
        if(scanResult.capabilities.contains("WEP")||scanResult.capabilities.contains("PSK")||
                scanResult.capabilities.contains("EAP")){
            tag.wifiLevel.setImageResource(R.drawable.wifi_level);
        }else{
            tag.wifiLevel.setImageResource(R.drawable.wifi_level);
        }
        tag.wifiLevel.setImageLevel(level);*/


        return convertView;
    }

    public void addAll(Collection<? extends ScanResult> collection){
        wifiList.addAll(collection);
        notifyDataSetChanged();
    }
    public static  class Viewholder{

        private final TextView ssid;ImageView wifiLevel;

        public  Viewholder(View item){
            ssid = ((TextView) item.findViewById(R.id.ssid));
            wifiLevel = (ImageView) item.findViewById(R.id.wifi_level);


        }
    }
}
